package com.sgcc.ln.pmos.e_ln_mall.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgcc.ln.pmos.arouter.mall.MallArouterPath;
import com.sgcc.ln.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseFragment;
import com.sgcc.ln.pmos.common.bean.BaseResponse;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.bean.PackageBean;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.ListPageUtil;
import com.sgcc.ln.pmos.common.utils.SmartLoadUtil;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.widget.LoadingLayout;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_mall.adapter.GoodsAdapter;
import com.sgcc.ln.pmos.e_ln_mall.adapter.GoodsTwoLinAdapter;
import com.sgcc.ln.pmos.e_ln_mall.bean.MallGoodsBean;
import com.sgcc.ln.pmos.e_ln_mall.contract.GoodsSearchContract;
import com.sgcc.ln.pmos.e_ln_mall.presenter.GoodsSearchPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品
 */
public class GoodsSearchFragment extends MvpBaseFragment<GoodsSearchPresenter> implements GoodsSearchContract.View, LoadingLayout.RetryListener {

    private LinearLayout mLlScreening;
    private SmartRefreshLayout mRefreshLayout;
    private LoadingLayout mLoadinglayout;
    private RecyclerView mRecycleview;
    private ImageView typeSwitchiImg;
    public BaseQuickAdapter adapter;
    public GoodsAdapter mGoodsAdapter;
    public GoodsTwoLinAdapter mGoodsTwoLinAdapter;
    public boolean isrefresh = false;//是否是刷新状态
    public List<PackageBean> mData = new ArrayList<>();
    private String searchContent;
    private ImageButton mIbPk;
    private ImageButton mIbMycollection;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_goods_search;
    }

    @Override
    protected GoodsSearchPresenter createPresenter() {
        return new GoodsSearchPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();
//        refresh(true);
    }

    private void findViewByIds() {
        mLlScreening = mRootView.findViewById(R.id.ll_screening);
        mRefreshLayout = mRootView.findViewById(R.id.refreshLayout);
        mLoadinglayout = mRootView.findViewById(R.id.loadinglayout);
        mRecycleview = mRootView.findViewById(R.id.recycleview);
        mIbPk = mRootView.findViewById(R.id.ib_pk);
        mIbMycollection = mRootView.findViewById(R.id.ib_mycollection);
        adapter = getAdapter(true);
        if (adapter != null) {
            mRecycleview.setAdapter(adapter);
        }
        mLoadinglayout.showContent();
        mLoadinglayout.setRetryListener(this);
        mRefreshLayout.setEnableLoadMore(true);
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh(false);
            }
        });
        mIbPk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //套餐对比
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_COMPARISONLIST)
                        .navigation();
            }
        });
        mIbMycollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //我的收藏
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_COLLECTION)
                            .navigation();
                }
            }
        });

    }

    public BaseQuickAdapter getAdapter(boolean showSingleLine) {
        mGoodsAdapter = new GoodsAdapter(mContext);
        mGoodsAdapter.setItemClickListener(new GoodsAdapter.OnItemClickListener() {
            @Override
            public void OnAddCollectionListener(int position, PackageBean bean) {
                mPresenter.addUserPackageCollection("", bean.getMembersSdId(), bean.getMembersSdName(),
                        bean.getCombId(), bean.getCombName());
            }

            @Override
            public void OnCancleCollectionListener(int position, PackageBean bean) {
                mPresenter.deletePackageById(bean.getCombId(), bean.getMembersSdId());
            }

            @Override
            public void OnItemsClickListener(int position, PackageBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_PACKAGEDETAILS)
                        .withString("combName",bean.getCombCode())
                        .withBoolean("isBlue", "1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                                "0".equals(bean.getMainItemType()) &&
                                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType())))

                        .navigation();
            }
        });
        return mGoodsAdapter;
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventMainThread(EventCenter eventCenter) {
        super.onEventMainThread(eventCenter);
        if (eventCenter.getEventCode() == ArguConstant.SELECT_CURVE_BACK) {
        }
        if (eventCenter.getEventCode() == ArguConstant.REFRESH_GOODS_LIST) {
            refresh(true);
        }

    }

    public void refresh(boolean isloading) {
        isrefresh = true;
        getDataRequest(0, isloading);
    }

    private void loadMore() {
        isrefresh = false;
        getDataRequest(mData.size(), false);
    }

    int mListSize;

    public void getDataRequest(int listSize, boolean isloading) {
        mListSize = listSize;
        mPresenter.getGoodsList(listSize, isloading,searchContent);
    }

    @Override
    public void onErrorclick() {
        refresh(true);
    }

    /**
     * @author yinsh
     * @description 请求成功
     * @date 2021/8/19 上午10:53
     */
    public void loadSuccess(boolean hasNext, List<PackageBean> datas) {
        if (isrefresh) {
            mData.clear();
        }
        if (datas != null) {
            mData.addAll(datas);
        }

        adapter.setNewData(mData);
        adapter.notifyDataSetChanged();
        SmartLoadUtil.canLoadMore(mData, hasNext, mRefreshLayout, mLoadinglayout, isrefresh, false);
    }

    /**
     * @author yinsh
     * @description 请求成功
     * @date 2021/8/19 上午10:52
     */
    public void loadFalied() {
        SmartLoadUtil.canFailLoadMore(mRefreshLayout, mLoadinglayout, isrefresh, false);
    }


    @Override
    public void queryGoodsListSuccess(MallGoodsBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null) {
                    loadSuccess(ListPageUtil.isHasNextPage(response.getData().getTotal(), mListSize), response.getData().getData());
                }
            } else {
                loadFalied();
            }
        }
    }

    @Override
    public void queryGoodsListFailed() {
        loadFalied();
    }

    @Override
    public void addCollectionSuccess(BaseResponse response) {
        if (response != null) {
            if (response.isSuccess()) {
                refresh(false);
            }
        }
    }

    @Override
    public void addCollectionFailed() {

    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
        refresh(true);
    }
}
